# -*- coding:utf-8 -*-

import os
import requests
import json

POST_JSON_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'}

class DxNwpApiClient(object):
    report_case_status_url = 'report_case_status/'

    def __init__(self, server='http://master/dxnwp', case_info=None):
        self.server = server
        if case_info:
            self.set_case(case_info)
        else:
            self.case = {}

    def set_case(self, case_info):
        self.case = case_info
        if 'report_site' in self.case:
            self.server = self.case['report_site']
        if 'report_case_status_url' in self.case:
            self.report_case_status_url = self.case['report_case_status_url']

    def report_case_status(self, status, info):
        if not self.case:
            return {"type": "NoCase"}

        data = {
            "type": "ReportCaseStatus",
            "casename": self.case['name'],
            "status": status,
            "info": info,
            "password": self.case['password']
        }
        r = requests.post(os.path.join(self.server, self.report_case_status_url), json.dumps(data), headers=POST_JSON_HEADERS)
        if r.status_code == 200:
            return json.loads(r.content, strict=False)
        else:
            return {"type": "BadReq"}



if __name__ == '__main__':
    c = DxNwpApiClient(server='http://localhost:8004')
    c.set_case({'name': 'fake/2015110812', 'password':'abc'})

    res = c.report_case_status('failed', 'abc')
    print res